/*
 * Author Hung Tran
 */
package viettravel.guiandevent.search;

import java.util.ArrayList;

import viettravel.DAO.PlaceDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import viettravel.guiandevent.place.ListPlaceAdapter;
import viettravel.guiandevent.place.PlaceDetail;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.actionbarsherlock.view.SubMenu;
import com.actionbarsherlock.widget.SearchView;

public class ListPlaceSearchActivity extends SherlockActivity implements
		SearchView.OnQueryTextListener, 
		SearchView.OnCloseListener{
    private ListView mListPlace;
    private ArrayList<PlaceDAO> mListRowPlace;
    private ListPlaceAdapter mListPlaceAdapter;
    //private boolean isAscending = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(Constants.THEME);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_list_place);
                
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setTitle("List search");
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		
		
        mListPlace = (ListView)findViewById(R.id.listPlace);
        mListPlace.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> _adapterView, View _view, int _position,
					long _viewId) {
				PlaceDAO r = (PlaceDAO) mListPlaceAdapter.getItem(_position);
				Intent intent = new Intent(ListPlaceSearchActivity.this, PlaceDetail.class);
				intent.putExtra(Constants.EXTRA_PLACE_ID, r.getId());
				startActivity(intent);
			}
		});
        
        mListRowPlace = new ArrayList<PlaceDAO>();
        
        /*
         * Query database to GUI
         */
        mListRowPlace = Constants.CURRENT_LIST_SEARCH;
        mListPlaceAdapter = new ListPlaceAdapter(mListRowPlace, this);
        mListPlace.setAdapter(mListPlaceAdapter);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu _menu) {
		
		SearchView mSearchView = new SearchView(getSupportActionBar().getThemedContext());
		mSearchView.setQueryHint("Search...");
        SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
		mSearchView.setSearchableInfo(searchManager.getSearchableInfo(this.getComponentName()));
		mSearchView.setIconifiedByDefault(false);
		mSearchView.setOnQueryTextListener(this);
		mSearchView.setOnCloseListener(this);

        _menu.add("Search")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(mSearchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

		
		SubMenu subMenuSort = _menu.addSubMenu(this.getString(R.string.submenu_control));
		subMenuSort.add(this.getString(R.string.sort_asc_vote)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				mListPlaceAdapter.ascSort();
				return false;
			}
		});
		
		subMenuSort.add(this.getString(R.string.sort_desc_vote)).setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				mListPlaceAdapter.decSort();
				return false;
			}
		});;
		
		MenuItem menuItemSort = subMenuSort.getItem();
		menuItemSort.setIcon(R.drawable.ic_list_w);
		menuItemSort.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return super.onCreateOptionsMenu(_menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:{
			finish();
            return true;
		}
		default:
			return super.onMenuItemSelected(featureId, item);
		}
	}

	@Override
	public boolean onClose() {
		// TODO Auto-generated method stub
		mListPlaceAdapter.filterData("");
		finish();
		finish();
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String _query) {
		mListPlaceAdapter.filterData(_query);
		return false;
	}

	@Override
	public boolean onQueryTextChange(String _query) {
		mListPlaceAdapter.filterData(_query);
		return false;
	}
}
